package seo.dale.swagger.book.dao;

import java.util.*;

import org.springframework.stereotype.Service;

import seo.dale.swagger.book.model.Book;
import seo.dale.swagger.book.status.OperationResult;

@Service
public class BookDAO implements DAO<Book, String> {

    @Override
    public OperationResult create(Book book) {
        OperationResult result = new OperationResult(OperationResult.Status.UNKNOWN, "");
        
        if (System.currentTimeMillis() % 2 == 0) {
        	result.setStatus(OperationResult.Status.SUCCESS);
            result.setMessage(String.format("The book  [%s] was successfully created", book));
        } else {
        	result.setStatus(OperationResult.Status.ERROR);
            result.setMessage(String.format("An error occurred while processing the book [%s]", book));
        }
        
        return result;
    }

    @Override
    public OperationResult<Book> update(String id, Book book) {
        OperationResult<Book> result = new OperationResult<Book>(OperationResult.Status.UNKNOWN, "");
        
        if (System.currentTimeMillis() % 2 == 0) {
            result.setStatus(OperationResult.Status.SUCCESS);
            result.setMessage(String.format("The request was successfully processed"));
            result.setData(book);
        } else {
            result.setStatus(OperationResult.Status.ERROR);
            result.setMessage(String.format("An error occurred while attempting to delete the book [%s]", id));
        }

        return result;
    }

    @Override
    public OperationResult<Book> delete(String id) {
        OperationResult result = new OperationResult(OperationResult.Status.UNKNOWN, "");
        if (System.currentTimeMillis() % 2 == 0) {
            result.setStatus(OperationResult.Status.SUCCESS);
            result.setMessage(String.format("The request was successfully processed"));
        } else {
            result.setStatus(OperationResult.Status.ERROR);
            result.setMessage(String.format("An error occurred while attempting to delete the book [%s]", id));
        }

        return result;
    }

    @Override
    public Book findOne(String id) {
    	Book book = new Book();
    	book.setId("10000001");
    	book.setAuthor("Dale Seo");
    	book.setComment("So good");
    	book.setPublicationYear(2001);
    	book.setTitle("Goodness");
    	return book;
    }

    @Override
    public List<Book> findAll() {
        List<Book> bookList = new ArrayList<Book>();
    	
    	for (int i = 0; i < 10; i++) {
    		Book book = new Book();
        	book.setId("1000000" + i);
        	book.setAuthor("Dale Seo" + i);
        	book.setComment(i + "So good" + i);
        	book.setPublicationYear(2001 + i);
        	book.setTitle(i + "Goodness");
        	bookList.add(book);
        }
    	
    	return bookList;
    }
}
